package net.chasing.androidbaseconfig.util;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class RotateAnimation3D extends Animation {
    private float mRotateCenterX, mRotateCenterY;
    private final Camera mCamera;

    private int mOffsetX = 0;
    private int mSelfDuration = 1000;
    private byte mRotateAxisType = ROTATE_AXIS_TYPE_X; //旋转轴位置类型
    private byte mRotateAxisPosition = ROTATE_AXIS_POSITION_START; //旋转轴位置
    private int mStartDegree; //开始旋转的角度
    private int mRotateDegree; //需要旋转的角度

    public static final byte ROTATE_AXIS_TYPE_X = 0;
    public static final byte ROTATE_AXIS_TYPE_Y = 1;
    public static final byte ROTATE_AXIS_TYPE_Z = 2;

    public static final byte ROTATE_AXIS_POSITION_START = 0;
    public static final byte ROTATE_AXIS_POSITION_CENTER = 1;
    public static final byte ROTATE_AXIS_POSITION_END = 2;

    public RotateAnimation3D() {
        mCamera = new Camera();
    }

    public void setRotateAxisType(byte rotateAxisType) {
        mRotateAxisType = rotateAxisType;
    }

    public void setRotateAxisPosition(byte rotateAxisPosition) {
        mRotateAxisPosition = rotateAxisPosition;
    }

    public void setStartDegree(int startDegree) {
        mStartDegree = startDegree;
    }

    public void setRotateDegree(int rotateDegree) {
        mRotateDegree = rotateDegree;
    }

    public void setOffsetX(int offsetX) {
        mOffsetX = offsetX;
    }

    public void duration(int duration) {
        mSelfDuration = duration;
    }

    @Override
    public void initialize(int width, int height, int parentWidth,
                           int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        //获取中心点坐标
        if (mRotateAxisType == ROTATE_AXIS_TYPE_X) {
            switch (mRotateAxisPosition) {
                case ROTATE_AXIS_POSITION_START:
                    mRotateCenterX = width + mOffsetX;
                    mRotateCenterY = 0;
                    break;
                case ROTATE_AXIS_POSITION_CENTER:
                    mRotateCenterX = width / 2f + mOffsetX;
                    mRotateCenterY = height / 2f;
                    break;
                case ROTATE_AXIS_POSITION_END:
                    mRotateCenterX = width + mOffsetX;
                    mRotateCenterY = height;
                    break;
            }
        } else if (mRotateAxisType == ROTATE_AXIS_TYPE_Y) {
            mRotateCenterY = height / 2f;
            switch (mRotateAxisPosition) {
                case ROTATE_AXIS_POSITION_START:
                    mRotateCenterX = mOffsetX;
                    break;
                case ROTATE_AXIS_POSITION_CENTER:
                    mRotateCenterX = width / 2f + mOffsetX;
                    break;
                case ROTATE_AXIS_POSITION_END:
                    mRotateCenterX = width + mOffsetX;
                    break;
            }
        }
        setDuration(mSelfDuration);
        setInterpolator(new AccelerateDecelerateInterpolator());//先加速后减速
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        Matrix matrix = t.getMatrix();
        mCamera.save();
        if (mStartDegree != 0) {
            //中心是绕Y轴旋转  这里可以自行设置X轴 Y轴 Z轴
            switch (mRotateAxisType) {
                case ROTATE_AXIS_TYPE_X:
                    mCamera.rotateX(mStartDegree);
                    break;
                case ROTATE_AXIS_TYPE_Y:
                    mCamera.rotateY(mStartDegree);
                    break;
                case ROTATE_AXIS_TYPE_Z:
                    mCamera.rotateZ(mStartDegree);
                    break;
            }
        }
        //中心是绕Y轴旋转  这里可以自行设置X轴 Y轴 Z轴
        switch (mRotateAxisType) {
            case ROTATE_AXIS_TYPE_X:
                mCamera.rotateX(mRotateDegree * interpolatedTime);
                break;
            case ROTATE_AXIS_TYPE_Y:
                mCamera.rotateY(mRotateDegree * interpolatedTime);
                break;
            case ROTATE_AXIS_TYPE_Z:
                mCamera.rotateZ(mRotateDegree * interpolatedTime);
                break;
        }
        //把我们的摄像头加在变换矩阵上
        mCamera.getMatrix(matrix);
        //物体偏移
        matrix.preTranslate(-mRotateCenterX, -mRotateCenterY);
        matrix.postTranslate(mRotateCenterX, mRotateCenterY);
        mCamera.restore();
    }
}